moduleCommon.service('ConstantService', function ($rootScope) {

    var self = this;
    var data = {};
    var session = null;

    /**
     * 判断是否支持本地存储
     * @returns {boolean}
     */
    var isLocalStorageSupported = function () {
        var testKey = 'test';
        session = window.sessionStorage;
        try {
            session.setItem(testKey, 'testValue');
            session.removeItem(testKey);
            return true;
        } catch (error) {
            return false;
        }
    };

    //select选项数据源，只复制第一层属性
    var copyOptions = function (source) {
        var result = {};
        if (source instanceof Object) {
            for (var key in source) {
                if (!(source[key] instanceof Object || source[key] instanceof Array)) {
                    result[key] = source[key];
                }
            }
        }
        return result;
    };

    var removeSessionItem = function (key) {
        if (isLocalStorageSupported()) {
            session.removeItem(key);
        }
    };

    var getSessionItem = function (key) {
        if (isLocalStorageSupported()) {
            var value = session.getItem(key);
            if (!!value && value != "undefined") {
                return JSON.parse(value);
            }
        }
        return null;
    };

    var setSessionItem = function (key, value, isOptions) {
        if (!!isOptions && value instanceof Array) {
            var valueOptions = [];
            angular.forEach(value, function (valueTemp) {
                valueOptions.push(copyOptions(valueTemp));
            });
            value = valueOptions;
        }
        if (isLocalStorageSupported()) {
            session.setItem(key, JSON.stringify(value));
        }
    };

    this.getItem = function (key) {
        return data[key] || getSessionItem(key);
    };

    this.setItem = function (key, value, isOptions) {
        data[key] = value;
        setSessionItem(key, value, isOptions);
    };

    this.removeItem = function (key) {
        delete data[key];
        removeSessionItem(key);
    };

    this.clear = function () {
        data = {};
        if (isLocalStorageSupported()) {
            // var locale = navigator.language;
            // var i18n = session.getItem(locale + '_language');
            var currentLanguage = session.getItem('currentLanguage');
            session.clear();
            session.setItem('currentLanguage', currentLanguage);
        }
    };

    this.addState = function (state, isPop, pageName) {
        $rootScope.stateTree = self.getItem("stateTree");
        var stateTreeTemp = [];
        var inTree = false;
        if (!isPop) {
            for (var i = 0; i < $rootScope.stateTree.length; i++) {
                stateTreeTemp.push($rootScope.stateTree[i]);
                if ($rootScope.stateTree[i].name == state.name) {
                    inTree = true;
                    break;
                }
            }
        }
        if (!inTree) {
            if (pageName) {
                state.pageName = pageName;
            }
            stateTreeTemp.push(state);
        }
        $rootScope.stateTree = stateTreeTemp;
        self.setItem("stateTree", stateTreeTemp);
    };

    this.clearState = function () {
        $rootScope.stateTree = [];
        self.setItem("stateTree", $rootScope.stateTree);
    };

});